#include "wsystemtray.h"
#include <QStyle>
#include <QApplication>

WSystemTray::WSystemTray(QWidget *parent) : QSystemTrayIcon(parent)
{
    parentWidget = parent;
    createMenu();
    initSysTray();

//    this->show();
    // 隐藏界面
    connect(hideaction, SIGNAL(triggered()), this, SLOT(hideParentWidget()));
    // 恢复
    connect(restoreaction, SIGNAL(triggered()), this, SLOT(showParentWidget()));
    // 退出程序
    connect(quitaction, SIGNAL(triggered()), qApp, SLOT(quit()) );
}

// 绘制菜单
void WSystemTray::createMenu()
{
    restoreaction = new QAction("显示界面", this);
    quitaction = new QAction("退出程序", this);
    hideaction = new QAction("隐藏界面", this);
    traymenu = new QMenu((QWidget*)QApplication::desktop());
    //添加菜单
    traymenu->addAction(hideaction);
    traymenu->addAction(restoreaction);
    //分隔符
    traymenu->addSeparator();
    traymenu->addAction(quitaction);

}

void WSystemTray::initSysTray()
{
    setToolTip(tr("系统托盘"));
    setIcon(QIcon(":/logo/obsidian128.ico"));	// 设置托盘图标
    // 鼠标停放在托盘图标，显示的信息
    showMessage("托盘","托盘管理",QSystemTrayIcon::Information,10000);
    setContextMenu(traymenu);
}
/*************************************************************************
 * 函数名：showTray
 * 函数功能描述：显示系统托盘
 ************************************************************************/
void WSystemTray::showTray()
{
    this->show();
}

void WSystemTray::showParentWidget()
{
    parentWidget->show();
}

void WSystemTray::hideParentWidget()
{
    parentWidget->hide();
}
